<?PHP
class Backend_ErrorController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	
	public function init()
	{		
		// registry
		$registry = Zend_Registry::getInstance();
		$this->registry = $registry;
		
		// auth
		$auth = Zend_Auth::getInstance();
    	$this->view->auth = $auth->hasIdentity();
    	
    	if($auth->hasIdentity())
    	{
    		$this->view->auth_role 			= $auth->getIdentity()->role;
    		$this->view->username 			= $auth->getIdentity()->username;
    		$this->view->auth_uid			= $auth->getIdentity()->uid;
    		$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
    	}
    	else
    	{
    		$this->view->username 	= $this->registry->get('lang')->_('guest');
    	}

    	$this->auth = $auth;
    	
		// lang
		$this->view->translate = Zend_Registry::get('lang');
	}
	
    public function errorAction()
    {
        $this->_helper->viewRenderer->setViewSuffix('phtml');
        $errors = $this->_getParam('error_handler');

        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = 'Page not found';
                $this->view->code    = 404;
                if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER) {
                    $this->view->info = sprintf(
                                            'Unable to find controller "%s" in module "%s"', 
                                            $errors->request->getControllerName(),
                                            $errors->request->getModuleName()
                                        );
                }
                if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION) {
                    $this->view->info = sprintf(
                                            'Unable to find action "%s" in controller "%s" in module "%s"', 
                                            $errors->request->getActionName(),
                                            $errors->request->getControllerName(),
                                            $errors->request->getModuleName()
                                        );
                }
                break;
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = 'Application error';
                $this->view->code    = 500;
                $this->view->info    = $errors->exception;
                break;
        }
    }
    
    public function deniedAction()
    {

    }
    
    public function bannedAction()
    {
    	// params
    	$thread_uid = $this->_request->getParam('thread_uid', false);
    	$board_uid 	= $this->_request->getParam('board_uid', false);
    	
    	// db
    	$boards		= new Boards();
    	$threads	= new Threads();
    	
    	// helper
    	$checkBan	= new MHDEV_View_Helper_IsBanned();
    	$date		= new MHDEV_View_Helper_Datetime();
    	
    	$result		= $checkBan->IsBanned($this->auth->getIdentity()->uid, $thread_uid, $board_uid);
    	
    	if($result[0]['board_uid'] != 0)
    	{
    		$getBoard 	= $boards->find($result[0]['board_uid']);
    		$resBoard	= $getBoard->toArray();
    		$level		= Zend_Registry::get('lang')->_('banned_board').' ' . htmlentities($resBoard[0]['title']);
      	}
      	else
      	{
      		$getThread 	= $threads->find($result[0]['thread_uid']);
    		$resThread	= $getThread->toArray();
    		$level		= Zend_Registry::get('lang')->_('banned_thread').' ' . htmlentities($resThread[0]['title']);
      	}
    	
    	$this->view->text = MHDEV_Sprintf(Zend_Registry::get('lang')->_('banned_reason'), array(
    		'%expire'	=> $date->datetime($result[0]['expire_date']),
    		'%reason'	=> $result[0]['reason'],
    		'%level'	=> $level
    	));
    }
    
    public function offlineAction()
    {
    	// settings
    	$settings = Zend_Registry::getInstance()->get('settings');
    	
    	$this->view->error_message = $settings['offline_message'];
    }
}